package com.joshua.ticket.manager.controllers;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.joshua.userservice.models.Continent;

@Controller
@RequestMapping("/rest")
public class RestfulController {

	@Autowired
	public SessionFactory sessionFactory;

	
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	public @ResponseBody
	JsonResponse delete(@RequestBody Continent continent) {
		JsonResponse jsonResponse = new JsonResponse();
		// String id = "999";
		try {
			// Continent continent = new Continent();
			// continent.setId(Long.parseLong(id));
			Session session = sessionFactory.openSession();
			Transaction tx = session.beginTransaction();
			session.delete(continent);
			tx.commit();
			session.close();

			jsonResponse.setException("None");
			jsonResponse.setResultState("Success");
			jsonResponse.setResult("Delete Continent with id:"
					+ continent.getId());
		} catch (Exception ex) {
			jsonResponse.setException(ex.toString());
			jsonResponse.setResultState("Failure");
			jsonResponse.setResult("Delete Continent with id:"
					+ continent.getId() + "Failure");
		}

		return jsonResponse;
	}

}
